﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageProcessing.IntermediateClasses;

namespace ImageProcessing
{
    public partial class AddingNoise : Form
    {
        private Image originalImage;
        private int countOriginal, countNoisy;

        public AddingNoise(Image originalImage)
        {
            InitializeComponent();
            this.originalImage = originalImage;
            this.pictureBoxOriginal.Image = this.originalImage;
            this.countNoisy = 0;
            this.countOriginal = 0;
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap temp = new Bitmap(this.pictureBoxOriginal.Image);
                Noise noise = new Noise(temp);
                double pepperProb = double.Parse(this.txtPepperProbability.Text);
                double saltProb = double.Parse(this.txtSaltProbability.Text);
                Bitmap result = noise.AddingSaltAndPepper(saltProb, pepperProb);
                this.pictureBoxResult.Image = result;
            }
            catch
            {
                MessageBox.Show("Error adding salt and pepper.");
            }
        }

        private void btnUnifrom_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap temp = new Bitmap(this.pictureBoxOriginal.Image);
                Noise noise = new Noise(temp);
                int a = int.Parse(this.txtA.Text);
                int b = int.Parse(this.txtB.Text);
                double probability = double.Parse(this.txtProbability.Text);

                this.pictureBoxResult.Image = noise.UniformAdditiveNoise(a, b, probability);
            }
            catch
            {
                MessageBox.Show("Error adding uniform noise.");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap temp = new Bitmap(this.pictureBoxOriginal.Image);
                Noise noise = new Noise(temp);
                int mu = int.Parse(this.txtMu.Text);
                int sigma = int.Parse(this.txtSigma.Text);
                double probability = double.Parse(this.txtGaussianProb.Text);

                this.pictureBoxResult.Image = noise.GaussianAdditiveNoise(mu, sigma, probability);
            }
            catch
            {
                MessageBox.Show("Error adding gaussian noise.");
            }
        }

        private void meanFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap temp = new Bitmap(this.pictureBoxResult.Image);
                Filtering filters = new Filtering(temp, int.Parse(this.txtWindowSize.Text));
                this.pictureBoxOriginal.Image = filters.MeanFilter();
            }
            catch
            {
                MessageBox.Show("Error applying geometric mean filter.");
            }
        }

        private void minFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap temp = new Bitmap(this.pictureBoxResult.Image);
                Filtering filters = new Filtering(temp, int.Parse(this.txtWindowSize.Text));
                this.pictureBoxOriginal.Image = filters.MinMaxMedianMidpoint(0);
            }
            catch
            {
                MessageBox.Show("Error applying  minimum filter.");
            }
        }

        private void maxFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap temp = new Bitmap(this.pictureBoxResult.Image);
                Filtering filters = new Filtering(temp, int.Parse(this.txtWindowSize.Text));
                this.pictureBoxOriginal.Image = filters.MinMaxMedianMidpoint(1);
            }
            catch
            {
                MessageBox.Show("Error applying  maximum filter.");
            }
        }

        private void medianFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap temp = new Bitmap(this.pictureBoxResult.Image);
                Filtering filters = new Filtering(temp, int.Parse(this.txtWindowSize.Text));
                this.pictureBoxOriginal.Image = filters.MinMaxMedianMidpoint(2);
            }
            catch
            {
                MessageBox.Show("Error applying  median filter.");
            }
        }

        private void midpointFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap temp = new Bitmap(this.pictureBoxResult.Image);
                Filtering filters = new Filtering(temp, int.Parse(this.txtWindowSize.Text));
                this.pictureBoxOriginal.Image = filters.MinMaxMedianMidpoint(3);
            }
            catch
            {
                MessageBox.Show("Error applying  midpoint filter.");
            }
        }

        private void btnSaveOriginal_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap original = new Bitmap(this.pictureBoxOriginal.Image);
                original.Save("Original" + this.countOriginal.ToString() + ".bmp");
                this.countOriginal++;
            }
            catch
            {
                MessageBox.Show("Error saving image.");
            }
        }
        
        private void btnSaveNoisy_Click(object sender, EventArgs e)
        {
            try
            {
                Bitmap noisy = new Bitmap(this.pictureBoxResult.Image);
                noisy.Save("Noisy" + this.countNoisy.ToString() + ".bmp");
                this.countNoisy++;
            }
            catch
            {
                MessageBox.Show("Error saving image.");
            }
        }
    }
}
